package com.zwxbc.funplay.dao.impl;

import com.zwxbc.funplay.dao.AdminDao;
import com.zwxbc.funplay.entity.Admin;
import com.zwxbc.funplay.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AdminDaoImpl implements AdminDao {

	private static final String INSERT_ADMIN = "INSERT INTO admin (admin_name, admin_username, admin_password, admin_contact, admin_email) VALUES (?, ?, ?, ?, ?)";
	private static final String SELECT_ADMIN_BY_ID = "SELECT * FROM admin WHERE admin_id = ?";
	private static final String SELECT_ALL_ADMINS = "SELECT * FROM admin";
	private static final String UPDATE_ADMIN = "UPDATE admin SET admin_name = ?, admin_username = ?, admin_password = ?, admin_contact = ?, admin_email = ? WHERE admin_id = ?";
	private static final String DELETE_ADMIN = "DELETE FROM admin WHERE admin_id = ?";
	private static final String LOGIN_ADMIN = "SELECT * FROM admin WHERE (admin_username = ? or admin_contact = ? or admin_email = ?) and admin_password=?";
	private static final String ADMIN_EXISTS = "SELECT COUNT(*) FROM admin WHERE admin_username = ? OR admin_contact = ? OR admin_email = ?";


	@Override
	public int addAdmin(Admin admin) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(INSERT_ADMIN)) {
			preparedStatement.setString(1, admin.getAdminName());
			preparedStatement.setString(2, admin.getAdminUsername());
			preparedStatement.setString(3, admin.getAdminPassword());
			preparedStatement.setString(4, admin.getAdminContact());
			preparedStatement.setString(5, admin.getAdminEmail());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}


	@Override
	public Admin adminLogin(Admin admin) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(LOGIN_ADMIN)) {
			preparedStatement.setString(1, admin.getAdminUsername());
			preparedStatement.setString(2, admin.getAdminContact());
			preparedStatement.setString(3, admin.getAdminEmail());
			preparedStatement.setString(4, admin.getAdminPassword());
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createAdminFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public Admin getAdminById(Long adminId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ADMIN_BY_ID)) {
			preparedStatement.setLong(1, adminId);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					return createAdminFromResultSet(resultSet);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return null;
	}

	@Override
	public List<Admin> getAllAdmins() {
		List<Admin> admins = new ArrayList<>();
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_ADMINS);
		     ResultSet resultSet = preparedStatement.executeQuery()) {
			while (resultSet.next()) {
				admins.add(createAdminFromResultSet(resultSet));
			}
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return admins;
	}

	@Override
	public int updateAdmin(Admin admin) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(UPDATE_ADMIN)) {
			preparedStatement.setString(1, admin.getAdminName());
			preparedStatement.setString(2, admin.getAdminUsername());
			preparedStatement.setString(3, admin.getAdminPassword());
			preparedStatement.setString(4, admin.getAdminContact());
			preparedStatement.setString(5, admin.getAdminEmail());
			preparedStatement.setLong(6, admin.getAdminId());
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public int deleteAdmin(Long adminId) {
		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(DELETE_ADMIN)) {
			preparedStatement.setLong(1, adminId);
			return preparedStatement.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace(); // 处理异常
		}
		return 0;
	}

	@Override
	public boolean isAdminExists(String username, String contact, String email) {
		int count = 0;

		try (Connection connection = DBUtil.getConnection();
		     PreparedStatement preparedStatement = connection.prepareStatement(ADMIN_EXISTS)) {
			preparedStatement.setString(1, username);
			preparedStatement.setString(2, contact);
			preparedStatement.setString(3, email);
			try (ResultSet resultSet = preparedStatement.executeQuery()) {
				if (resultSet.next()) {
					count = resultSet.getInt(1);
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return count > 0;
	}

	// Helper 方法，从 ResultSet 中创建 Admin 对象
	private Admin createAdminFromResultSet(ResultSet resultSet) throws SQLException {
		Admin admin = new Admin();
		admin.setAdminId(resultSet.getLong("admin_id"));
		admin.setAdminName(resultSet.getString("admin_name"));
		admin.setAdminUsername(resultSet.getString("admin_username"));
		admin.setAdminPassword(resultSet.getString("admin_password"));
		admin.setAdminContact(resultSet.getString("admin_contact"));
		admin.setAdminEmail(resultSet.getString("admin_email"));
		return admin;
	}

}
